
package biblio.service.reader;


import biblio.dao.ReaderDAO;
import biblio.domain.Reader;
import biblio.util.SourceName;
import java.io.Serializable;
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;
import org.apache.log4j.Logger;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;


/**
 * Provides data to editing and deleting.
 * 
 * @author mephisto
 */
@ManagedBean(name = "readerList")
@ViewScoped
public class ReaderList implements Serializable
{
    public void remove()
    {
        Reader tmp = allItems.get(currentReaderIndex);
        readerService.delete(tmp);
        allItems.remove(tmp);
    }

    public void store()
    {
        readerService.update(editedReader);
        allItems.set(currentReaderIndex, editedReader);
    }

    public List<Reader> getAllItems()
    {
        synchronized (this)
        {
            if (allItems == null)
            {
                ReaderDAO readerDAO = (ReaderDAO)appContext.getBean("readerDAO");
                allItems = readerDAO.selectAll();
            }
        }
        return allItems;
    }

    public int getCurrentReaderIndex()
    {
        return currentReaderIndex;
    }

    public void setCurrentReaderIndex(int currentReaderIndex)
    {
        this.currentReaderIndex = currentReaderIndex;
    }

    public Reader getEditedReader()
    {
        return editedReader;
    }

    public void setEditedReader(Reader editedReader)
    {
        this.editedReader = editedReader;
    }

    public int getPage()
    {
        return page;
    }

    public void setPage(int page)
    {
        this.page = page;
    }

    public void setReaderService(ReaderServiceImpl readerService)
    {
        this.readerService = readerService;
    }

    static
    {
        appContext = new ClassPathXmlApplicationContext( new String[] {
                SourceName.DAO_APPLICATION_CONTEXT
        });
    }


    private static final long serialVersionUID = -3832235132261771583L;
    private static ApplicationContext appContext;
    private static Logger log = Logger.getLogger(ReaderList.class);
    private int page = 1;
    private List<Reader> allItems = null;
    private int currentReaderIndex;
    private Reader editedReader;
    @ManagedProperty(value="#{readerService}")
    private ReaderServiceImpl readerService;
}
